namespace RSSReader.DAL.Migrations
{
  using System;
  using System.Data.Entity.Migrations;

  public partial class InitialCreateDataBase : DbMigration
  {
    public override void Up()
    {

      #region AspNet

      #region AspNetRoles

      CreateTable(
              "dbo.AspNetRoles",
              c => new
                  {
                    Id = c.String(nullable: false, maxLength: 128),
                    Name = c.String(nullable: false, maxLength: 256),
                  })
              .PrimaryKey(t => t.Id)
              .Index(t => t.Name, unique: true, name: "RoleNameIndex");

      #endregion

      #region AspNetUserRoles

      CreateTable(
          "dbo.AspNetUserRoles",
          c => new
              {
                UserId = c.String(nullable: false, maxLength: 128),
                RoleId = c.String(nullable: false, maxLength: 128),
              })
          .PrimaryKey(t => new { t.UserId, t.RoleId })
          .ForeignKey("dbo.AspNetRoles", t => t.RoleId, cascadeDelete: true)
          .ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
          .Index(t => t.UserId)
          .Index(t => t.RoleId);

      #endregion

      #region AspNetUsers

      CreateTable(
          "dbo.AspNetUsers",
          c => new
              {
                Id = c.String(nullable: false, maxLength: 128),
                Email = c.String(maxLength: 256),
                EmailConfirmed = c.Boolean(nullable: false),
                PasswordHash = c.String(),
                SecurityStamp = c.String(),
                PhoneNumber = c.String(),
                PhoneNumberConfirmed = c.Boolean(nullable: false),
                TwoFactorEnabled = c.Boolean(nullable: false),
                LockoutEndDateUtc = c.DateTime(),
                LockoutEnabled = c.Boolean(nullable: false),
                AccessFailedCount = c.Int(nullable: false),
                UserName = c.String(nullable: false, maxLength: 256),
              })
          .PrimaryKey(t => t.Id)
          .Index(t => t.UserName, unique: true, name: "UserNameIndex");

      #endregion

      #region AspNetUserClaims

      CreateTable(
          "dbo.AspNetUserClaims",
          c => new
              {
                Id = c.Int(nullable: false, identity: true),
                UserId = c.String(nullable: false, maxLength: 128),
                ClaimType = c.String(),
                ClaimValue = c.String(),
              })
          .PrimaryKey(t => t.Id)
          .ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
          .Index(t => t.UserId);

      #endregion

      #region AspNetUserLogins

      CreateTable(
          "dbo.AspNetUserLogins",
          c => new
              {
                LoginProvider = c.String(nullable: false, maxLength: 128),
                ProviderKey = c.String(nullable: false, maxLength: 128),
                UserId = c.String(nullable: false, maxLength: 128),
              })
          .PrimaryKey(t => new { t.LoginProvider, t.ProviderKey, t.UserId })
          .ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
          .Index(t => t.UserId);

      #endregion

      #endregion

      #region Rss

      #region Source

      CreateTable(
              "dbo.Sources",
              c => new
              {
                Id = c.Int(nullable: false, identity: true),
                Url = c.String(nullable: false, maxLength: 2083),
              })
              .PrimaryKey(t => t.Id)
              .Index(t => t.Url, unique: true);

      #endregion

      #region Articles
      CreateTable(
              "dbo.Articles",
              c => new
              {
                Id = c.Int(nullable: false, identity: true),
                SourceId = c.Int(nullable: false),
                Url = c.String(nullable: false, maxLength: 2083),
              })
              .PrimaryKey(t => t.Id)
              .ForeignKey("dbo.Sources", t => t.SourceId, cascadeDelete: true)
              .Index(t => t.SourceId)
              .Index(t => t.Url, unique: true);
      #endregion

      #region Subcription

      CreateTable(
          "dbo.Subscriptions",
          c => new
          {
            UserId = c.String(nullable: false, maxLength: 128),
            SourceId = c.Int(nullable: false),
            IsSubscribe = c.Boolean(nullable: false),
          })
          .PrimaryKey(t => new { t.UserId, t.SourceId })
          .ForeignKey("dbo.Sources", t => t.SourceId, cascadeDelete: true)
          .ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
          .Index(t => t.UserId)
          .Index(t => t.SourceId);

      #endregion

      #region UserArticles

      CreateTable(
          "dbo.UserArticles",
          c => new
              {
                UserId = c.String(nullable: false, maxLength: 128),
                ArticleId = c.Int(nullable: false),
                IsFavorite = c.Boolean(nullable: false),
              })
          .PrimaryKey(t => new { t.UserId, t.ArticleId })
          .ForeignKey("dbo.Articles", t => t.ArticleId, cascadeDelete: true)
          .ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
          .Index(t => t.UserId)
          .Index(t => t.ArticleId);

      #endregion

      #endregion

    }

    public override void Down()
    {
      DropForeignKey("dbo.UserArticles", "UserId", "dbo.AspNetUsers");
      DropForeignKey("dbo.UserArticles", "ArticleId", "dbo.Articles");
      DropForeignKey("dbo.Subscriptions", "UserId", "dbo.AspNetUsers");
      DropForeignKey("dbo.AspNetUserRoles", "UserId", "dbo.AspNetUsers");
      DropForeignKey("dbo.AspNetUserLogins", "UserId", "dbo.AspNetUsers");
      DropForeignKey("dbo.AspNetUserClaims", "UserId", "dbo.AspNetUsers");
      DropForeignKey("dbo.Subscriptions", "SourceId", "dbo.Sources");
      DropForeignKey("dbo.AspNetUserRoles", "RoleId", "dbo.AspNetRoles");
      DropForeignKey("dbo.Articles", "SourceId", "dbo.Sources");
      DropIndex("dbo.UserArticles", new[] { "ArticleId" });
      DropIndex("dbo.UserArticles", new[] { "UserId" });
      DropIndex("dbo.AspNetUserLogins", new[] { "UserId" });
      DropIndex("dbo.AspNetUserClaims", new[] { "UserId" });
      DropIndex("dbo.AspNetUsers", "UserNameIndex");
      DropIndex("dbo.Subscriptions", new[] { "SourceId" });
      DropIndex("dbo.Subscriptions", new[] { "UserId" });
      DropIndex("dbo.AspNetUserRoles", new[] { "RoleId" });
      DropIndex("dbo.AspNetUserRoles", new[] { "UserId" });
      DropIndex("dbo.AspNetRoles", "RoleNameIndex");
      DropIndex("dbo.Sources", new[] { "Url" });
      DropIndex("dbo.Articles", new[] { "Url" });
      DropIndex("dbo.Articles", new[] { "SourceId" });
      DropTable("dbo.UserArticles");
      DropTable("dbo.AspNetUserLogins");
      DropTable("dbo.AspNetUserClaims");
      DropTable("dbo.AspNetUsers");
      DropTable("dbo.Subscriptions");
      DropTable("dbo.AspNetUserRoles");
      DropTable("dbo.AspNetRoles");
      DropTable("dbo.Sources");
      DropTable("dbo.Articles");
    }
  }
}
